/*
	Personal Bug Tracker (www.personalbugtracker.com)
	Copyright (C) 2014 Sergey Nechaev <serg.nechaev@gmail.com>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
package com.personalbugtracker;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.personalbugtracker.service.Services;
import com.personalbugtracker.ui.controller.MainFrameFXController;

public class PersonalBugTracker extends Application {

	private static final Log log = LogFactory.getLog(PersonalBugTracker.class);

	private static ClassPathXmlApplicationContext ctx;

	private static MainFrameFXController mainFrameFXController;
	
	public static Services services;

	@Override
	public void start(Stage stage) {
		
		initSpring();

		Parent root = null;
		
		try {

			root = loadRootNode("/fxml/main-frame.fxml", "i18n.MainFrameBundle");

			mainFrameFXController.loadProjectsTree();
			
		} catch (IOException e) {
			log.error("Error: ", e);
		}

		//initSpring();
		stage.setTitle("Personal Bug Tracker (www.PersonalBugTracker.com)");

		stage.setScene(new Scene(root, 800, 600));

		stage.show();

	}

	protected Parent loadRootNode(String u, String bundleName) throws IOException {

		Parent root = null;

		URL location = getClass().getResource(u);

		FXMLLoader fxmlLoader = new FXMLLoader();
		fxmlLoader.setLocation(location);
		fxmlLoader.setResources(ResourceBundle.getBundle(bundleName));
		fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());

		root = (Parent) fxmlLoader.load(location.openStream());

		mainFrameFXController = fxmlLoader.getController();
		
		return root;
	}

	protected void initSpring() {

		ctx = new ClassPathXmlApplicationContext("/com/personalbugtracker/spring-context.xml");
		ctx.registerShutdownHook();
		ctx.start();
		services =  ctx.getBean(Services.class);
	}

	@Override
	public void stop() throws Exception {
		super.stop();
		if (ctx != null) {
			ctx.stop();
		}
	}

	public static void main(String[] args) {
		Application.launch();
	}

	public static void reloadProjectsTree() {
		mainFrameFXController.loadProjectsTree();
	}

}
